package cn.zeal4j;

import cn.zeal4j.util.springmail.MailOrderManager;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

/**
 * @author Administrator
 * @file IntelliJ IDEA Spring-Mail
 * @create 2020 09 22 10:39
 */
public class JavaMail {

    @Test
    public void sendNormalTextEmail() throws Exception{

        // 邮件地址配置
        Properties properties = new Properties();
        // IP设置
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        // 端口设置
        properties.setProperty("mail.smtp.port", "587"); // 465或者587 // javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
        // 身份认证设置
        properties.setProperty("mail.smtp.auth", "true");

        // 得到邮件会话实例
        Session emailSession = Session.getInstance(properties);
        // 开启调试模式
        emailSession.setDebug(true);
        // 获取传输对象
        Transport transport = emailSession.getTransport();
        // 由传输对象进行连接：
        transport.connect(properties.getProperty("mail.smtp.host"), "zeal4j", "frcgbdjqjzptbegg");

        // 邮件对象
        MimeMessage mimeMessage = new MimeMessage(emailSession);
        // 发送人设置
        mimeMessage.setFrom("zeal4j@qq.com");
        // 发送类型： Message.RecipientType
        // TO 发送 CC 抄送 BCC 加密抄送
        // 这里设置的是发件人和收件人为同一个人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("zeal4j@qq.com"));
        // 主题文本内容设置
        mimeMessage.setSubject("JavaMail邮件发送测试！！！");
        // 发送日期设置
        mimeMessage.setSentDate(new Date());
        // 内容设置
        mimeMessage.setText("演示文本。。。。 测试用");

        // 发送邮件
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        // 资源释放
        transport.close();
    }

    @Test
    public void sendHtmlEmail() throws Exception {
        // 配置信息脱离硬编码，放在配置文件中
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("email.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        // 得到邮件会话实例
        Session emailSession = Session.getInstance(properties);

        emailSession.setDebug(true);
        Transport transport = emailSession.getTransport();
        transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));

        // HTML邮件创建 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        MimeMessage mimeMessage = new MimeMessage(emailSession);
        mimeMessage.setFrom(properties.getProperty("mail.smtp.sender"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("mail.smtp.recipient")));

        mimeMessage.setSubject("JavaMail邮件发送测试！！！");
        mimeMessage.setSentDate(new Date());

        // 创建多媒体容器对象
        Multipart multipart = new MimeMultipart();
        MimeBodyPart mimeBodyPart = new MimeBodyPart();

        // HTML代码
        final String htmlCode =
                "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <h2>Hello Html Email</h2>\n" +
                "\n" +
                "    <p>testing text in here 测试Html邮件</p>\n" +
                "</body>\n" +
                "</html>";

        mimeBodyPart.setContent(htmlCode, "text/html;charset=utf-8");
        multipart.addBodyPart(mimeBodyPart);
        mimeMessage.setContent(multipart);
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        // 发送邮件
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        // 资源释放
        transport.close();
    }

    @Test
    public void sendIncludeAttachmentsEmail() throws Exception {
        // 配置信息脱离硬编码，放在配置文件中
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("email.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        // 得到邮件会话实例
        Session emailSession = Session.getInstance(properties);

        emailSession.setDebug(true);
        Transport transport = emailSession.getTransport();
        transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));

        MimeMessage mimeMessage = new MimeMessage(emailSession);
        mimeMessage.setFrom(properties.getProperty("mail.smtp.sender"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("mail.smtp.recipient")));

        mimeMessage.setSubject("JavaMail带附件邮件发送测试！！！");
        mimeMessage.setSentDate(new Date());

        // 创建多媒体容器对象
        Multipart multipart = new MimeMultipart();
        MimeBodyPart mimeBodyPart = new MimeBodyPart();

        // HTML代码，也可以不输入HTML，普通文本也支持
        final String htmlCode =
                "<!DOCTYPE html>\n" +
                        "<html lang=\"en\">\n" +
                        "<head>\n" +
                        "    <meta charset=\"UTF-8\">\n" +
                        "    <title>Title</title>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "    <h2>Hello Html Email</h2>\n" +
                        "\n" +
                        "    <p>附件测试</p>\n" +
                        "</body>\n" +
                        "</html>";

        mimeBodyPart.setContent(htmlCode, "text/html;charset=utf-8");
        multipart.addBodyPart(mimeBodyPart);
        mimeMessage.setContent(multipart);
        // 附件创建 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        // 需要重新的一个BodyPart对象
        MimeBodyPart attachmentBodyPart = new MimeBodyPart();

        // 文件源对象创建
        FileDataSource fileDataSource = new FileDataSource("D:\\picture\\collection\\330624.jpg");
        // 数据处理器
        DataHandler dataHandler = new DataHandler(fileDataSource);
        // 设置附件
        attachmentBodyPart.setDataHandler(dataHandler);
        // 设置附件名称
        attachmentBodyPart.setFileName(dataHandler.getName());
        // 装填附件Part
        multipart.addBodyPart(attachmentBodyPart);
        // 追加类型为混合的
        // multipart.setSubType("mixed");

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        // 发送邮件
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        // 资源释放
        transport.close();
    }

    @Test
    public void sendSpringMail() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");
        MailOrderManager mailOrderManager = applicationContext.getBean("mailOrderManager", MailOrderManager.class);
        mailOrderManager.placeOrder();
    }
    @Test
    public void sendSpringMailWithAttachment() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app.xml");

        JavaMailSender mailSender = applicationContext.getBean("mailSender", JavaMailSender.class);

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        mimeMessage.setSubject("Spring-Mail 附件测试");

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");

        mimeMessageHelper.setTo("zeal4j@qq.com");

        mimeMessageHelper.setFrom("zeal4j@qq.com");

        mimeMessageHelper.setText("Spring-Mail 附件测试 ......");

        File file = new File("D:\\picture\\collection\\bg.jpg");

        mimeMessageHelper.addAttachment(file.getName(), file);

        mailSender.send(mimeMessage);
    }
}
